package com.dsr.service.impl;

import cn.hutool.core.date.DateTime;
import com.dsr.consts.Result;
import com.dsr.dao.OrdersDao;
import com.dsr.entity.Collect;
import com.dsr.service.CollectService;
import com.dsr.service.DesktopService;
import com.dsr.service.OrdersService;
import com.dsr.service.SpitService;
import com.dsr.util.BasicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author dsr
 * @since 2020-10-19
 */
@Service("desktopService")
public class DesktopServiceImpl implements DesktopService {
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private CollectService collectService;
    @Autowired
    private SpitService spitService;
    @Override
    public Result saleSum() {
        Map<String, Integer> saleSum = ordersDao.getSaleSum();
        return Result.success(saleSum);
    }

    @Override
    public Result daysum() {
        List<Map<String , Integer>> maps = ordersDao.daySum();
        Map<String, Integer> days = BasicUtil.getDays();
        maps.forEach(item->{
                days.put(item.get("days")+"",item.get("count"));
        });
        return Result.success(days);
    }

    @Override

    public Result mouthSum() {
        Integer[] current =new Integer[12];
        Integer[] lastYear =new Integer[12];
        Arrays.fill(current,0);
        Arrays.fill(lastYear,0);
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        List<Map<String, Object>> maps = ordersDao.currentYearCount(instance.get(Calendar.YEAR) , instance.get(Calendar.YEAR));
        maps.forEach(item->{
            Integer number= Integer.parseInt(item.get("number").toString());
            Integer months= Integer.parseInt(item.get("months").toString());
            current[months-1]=number;
        });
        List<Map<String, Object>> mapsLastYear = ordersDao.currentYearCount(instance.get(Calendar.YEAR) - 1, instance.get(Calendar.YEAR)-1);
        mapsLastYear.forEach(item->{
            Integer number= Integer.parseInt(item.get("number").toString());
            Integer months= Integer.parseInt(item.get("months").toString());
            lastYear[months-1]=number;
        });
        List<Integer[]> list=new ArrayList<>();
        list.add(current);
        list.add(lastYear);
        return Result.success(list);
    }

    @Override
    public Result commitSum() {
        return spitService.commitSum();
    }

    @Override
    public Result saleTop() {
        return Result.success( ordersDao.saleTop());
    }

    @Override
    public Result collectTop() {
        return collectService.collectTop();
    }

    @Override
    public Result shopTop() {
        return Result.success(ordersDao.shopTop());
    }
}
